import AllCopCfgPanel from "@/components/AllCopCfgPanel";
import RulePanel from "@/components/RulePanel";
import { Config, genDefaultConfig } from "@/components/config";
import { BundlerRules } from "@/components/rules/bundler";
import { GemspecRules } from "@/components/rules/gemspec";
import { LayoutRules } from "@/components/rules/layout";
import { LintRules } from "@/components/rules/lint";
import { MetricsRules } from "@/components/rules/metrics";
import { MigrationRules } from "@/components/rules/migration";
import { NamingRules } from "@/components/rules/naming";
import { SecurityRules } from "@/components/rules/security";
import { StyleRules } from "@/components/rules/style";
import { Button, Card, Space, Tabs, message } from "antd";
import { useState } from "react";
import useForceUpdate from 'use-force-update';
import { min_app_fs, min_app_shell } from '@linksaas-minapp/api';
import YAML from 'yaml'

export default function Home() {
  const forceUpdate = useForceUpdate();
  const [ruboCfg, setRuboCfg] = useState(genDefaultConfig());

  const openFile = async () => {
    let files = await min_app_fs.readFile(
      false,
      "yaml文件", ["yml", "yaml"]
    );
    if (files.length == 0) {
      return;
    }
    try {
      const content = new TextDecoder("utf-8").decode(files[0].file_data);
      const config = YAML.parse(content) as unknown as Config;
      setRuboCfg(config);
    } catch (e) {
      console.log(e);
      message.error("加载配置失败");
    }
  };

  const saveFile = async () => {
    const content = YAML.stringify(ruboCfg);
    const data = new TextEncoder().encode(content);
    await min_app_fs.writeFile(".rubocop.yml", data);
  }


  return (
    <Card title="Rubocop配置助手" extra={
      <Space>
        <Button type="link" onClick={e => {
          e.stopPropagation();
          e.preventDefault();
          min_app_shell.openBrowser("https://rubocop.org/");
        }}>官方网站</Button>
        <Button onClick={e => {
          e.stopPropagation();
          e.preventDefault();
          setRuboCfg(genDefaultConfig());
          forceUpdate();
        }}>重置配置</Button>
        <Button onClick={e => {
          e.stopPropagation();
          e.preventDefault();
          openFile();
        }}>导入</Button>
        <Button onClick={e => {
          e.stopPropagation();
          e.preventDefault();
          saveFile();
        }}>导出</Button>
      </Space>
    }>
      <Tabs defaultActiveKey="all">
        <Tabs.TabPane key="all" tab="全局配置">
          <AllCopCfgPanel allCops={ruboCfg.AllCops} onChange={newValue => {
            setRuboCfg({ ...ruboCfg, AllCops: newValue });
            forceUpdate();
          }} />
        </Tabs.TabPane>
        <Tabs.TabPane key="bundler" tab="Bundler">
          <RulePanel config={ruboCfg} ruleList={BundlerRules} onChange={config => {
            setRuboCfg(config);
            forceUpdate();
          }} />
        </Tabs.TabPane>
        <Tabs.TabPane key="gemspec" tab="Gemspec">
          <RulePanel config={ruboCfg} ruleList={GemspecRules} onChange={config => {
            setRuboCfg(config);
            forceUpdate();
          }} />
        </Tabs.TabPane>
        <Tabs.TabPane key="layout" tab="Layout">
          <RulePanel config={ruboCfg} ruleList={LayoutRules} onChange={config => {
            setRuboCfg(config);
            forceUpdate();
          }} />
        </Tabs.TabPane>
        <Tabs.TabPane key="lint" tab="Lint">
          <RulePanel config={ruboCfg} ruleList={LintRules} onChange={config => {
            setRuboCfg(config);
            forceUpdate();
          }} />
        </Tabs.TabPane>
        <Tabs.TabPane key="metrics" tab="Metrics">
          <RulePanel config={ruboCfg} ruleList={MetricsRules} onChange={config => {
            setRuboCfg(config);
            forceUpdate();
          }} />
        </Tabs.TabPane>
        <Tabs.TabPane key="migration" tab="Migration">
          <RulePanel config={ruboCfg} ruleList={MigrationRules} onChange={config => {
            setRuboCfg(config);
            forceUpdate();
          }} />
        </Tabs.TabPane>
        <Tabs.TabPane key="naming" tab="Naming">
          <RulePanel config={ruboCfg} ruleList={NamingRules} onChange={config => {
            setRuboCfg(config);
            forceUpdate();
          }} />
        </Tabs.TabPane>
        <Tabs.TabPane key="security" tab="Security">
          <RulePanel config={ruboCfg} ruleList={SecurityRules} onChange={config => {
            setRuboCfg(config);
            forceUpdate();
          }} />
        </Tabs.TabPane>
        <Tabs.TabPane key="style" tab="Style">
          <RulePanel config={ruboCfg} ruleList={StyleRules} onChange={config => {
            setRuboCfg(config);
            forceUpdate();
          }} />
        </Tabs.TabPane>
      </Tabs>
    </Card>
  )
}
